home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2040 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  52 lines

  1. Path: thor.tu.hac.com!collins
  2. From: collins@thor.tu.hac.com (Ron Collins)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help, best way to compare doubles
  5. Date: 18 Jan 1996 17:46:15 GMT
  6. Organization: Advanced Depot Systems
  7. Message-ID: <4dm117$6a6@hacgate2.hac.com>
  8. References: <4da169$mm4@mercury.IntNet.net> <4dfdtl$phv@nntp.crl.com> <DLD5Kp.JME@eskimo.com>
  9. NNTP-Posting-Host: thor.tu.hac.com
  10.  
  11. mAg (mag@eskimo.com) wrote:
  12. : In article <4dfdtl$phv@nntp.crl.com> (16 Jan 1996 05:43:17 GMT), 
  13. : bivey@ecf2.puc.edu says :
  14. : >
  15. : >Jeff Tomich (jtomich@IntNet.net) wrote:
  16. : >: Looking for a way to compare doubles for equality.
  17. : >
  18. : >: thanks, Jeff
  19. : >
  20. : >The first question to ask is "how equal"?  In general, one picks some
  21. : >error limit, such as  e = 1E-6;   then uses it in this form:
  22. : >
  23. : >   if (fabs(n1 - n2) < e)  // then they are considered equal
  24. : >   else                    // they are considered different
  25. : >
  26.  
  27. : "How equal" is irrelevant. "Equal to" comparison produces a boolean result 
  28. : True or False. 
  29.  
  30. : '==' is good enough
  31.  
  32. This is fine if you are comparing for *exact bit pattern* matches.  If,
  33. OTOH, you have done a significant amount of number crunching, then it
  34. is only prudent to anticipate a certain amount of propagated round-off
  35. error.  In this case, you may wish to consider n1 and n2 "equal" (representing
  36. the same value), without expecting the same bit pattern stored in the
  37. variables.  For these cases, using an epsilon compare (fabs(n1 - n2) < e)
  38. is much more meaningfull than a flat "==" comparison.
  39.  
  40. "How equal" is indeed relevant.
  41.  
  42.  
  43.  
  44.                         -- Collins --
  45.                         
  46. -----
  47. The views expressed here are mine alone.
  48.  
  49. Ron Collins/Hughes Aircraft Company/M20,P20/Tucson Az 85706
  50. rcollins@thor.tu.hac.com    collins@seagull.rtd.com
  51. ยก----
  52.